
package ao.com.bna.util;

import java.util.List;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;
import javax.servlet.ServletContext;

public final class JsfUtil {
	public static final String ESTADO_PESQUISAR = "pesquisar";
	public static final String ESTADO_ADICIONAR = "adicionar";
	public static final String ESTADO_ACTUALIZAR = "editar";
	public static final String ESTADO_ELIMINAR = "eliminar";
	public static final String ESTADO_DETALHES = "detalhes";
	public static final String ESTADO_FORMVAZIO = "vazio";
    
    public static FacesContext getFacesContext(){  
        return FacesContext.getCurrentInstance();  
    }
    
    public static Application getApplication(){  
        return JsfUtil.getFacesContext().getApplication(); 
    } 
    
    public static ExternalContext getExternalContext(){  
        return JsfUtil.getFacesContext().getExternalContext(); 
    }  
    
    public static ServletContext getServletContext(){  
        return (ServletContext)JsfUtil.getExternalContext().getContext(); 
    }  
    
    public static SelectItem[] getSelectItemsVazia() {
        SelectItem[] items = {new SelectItem(null, "---")};
        return items;
    }
    
    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem(null, "---");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;
    }
    
    public static void addErrorMessage(Exception ex, String defaultMsg) {
        String msg = ex.getLocalizedMessage();
        if (msg != null && msg.length() > 0) {
            addErrorMessage(msg);
        } else {
            addErrorMessage(defaultMsg);
        }
    }
    
    public static void addErrorMessages(List<String> messages) {
        for (String message : messages) {
            addErrorMessage(message);
        }
    }

    public static void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
        //FacesContext.getCurrentInstance().addMessage("Mistakes Error", facesMsg);  
    }

    public static void addSuccessMessage(String msg) {
        //FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        //FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
        addInfoMessage(msg);
    }
    
    /////
    public static void addInfoMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext.getCurrentInstance().addMessage("Info Message", facesMsg); 
    }  
  
    public static void addWarnMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_WARN, msg, msg);
        FacesContext.getCurrentInstance().addMessage("Warn Message", facesMsg); 
    }
  
    public static void addFatalErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, msg, msg);
        FacesContext.getCurrentInstance().addMessage("Fatal Error", facesMsg);   
    }  
    /////////
    
    public static String getRequestParameter(String key) {
        return FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
    }
    
    public static Object getObjectFromRequestParameter(String requestParameterName, Converter converter, UIComponent component) {
        String theId = JsfUtil.getRequestParameter(requestParameterName);
        return converter.getAsObject(FacesContext.getCurrentInstance(), component, theId);
    }
    
}